package cn.sunline.shengpizi;

import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;

public class UnicodeUtil {

    /**
     * 判断字符串里是否有生僻字
     */
    public static Boolean isShengpizi(String org) throws UnsupportedEncodingException {
        return !org.equals(new String(org.getBytes("gb18030"),"gb2312"));
    }


    /**
     * 汉字转换Unicode
     * @param str
     * @return
     */
    public static String StringToUnicode(String str) {
        char[] arChar = str.toCharArray();
        int iValue = 0;
        String uStr = "";
        for (int i = 0; i < arChar.length; i++) {
            iValue = (int) str.charAt(i);
            if (iValue <= 256) {
                // uStr+="& "+Integer.toHexString(iValue)+";";
                uStr += "\\" + Integer.toHexString(iValue);
            } else {
                // uStr+="&#x"+Integer.toHexString(iValue)+";";
                uStr += "\\u" + Integer.toHexString(iValue);
            }
        }
        return uStr;
    }

    /**
     * Unicode转换中文（可以处理特殊符号）
     * @param str
     * @return
     */
    public static String UnicodeToString(String str) {
        StringBuffer sb = new StringBuffer();
        StringTokenizer st = new StringTokenizer(str, "\\u");
        while (st.hasMoreTokens()) {
            sb.append((char) Integer.parseInt(st.nextToken(), 16));
        }
        return sb.toString();
    }

    /**
     * 中文转Unicode，可以处理特殊符号
     * @param str
     * @return
     */
    public static String chinaToUnicode(String str) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                sb.append("\\u" + Integer.toHexString(c));
            }
        }
        return sb.toString();
    }

    /**
     * Unicode转中文
     * @param utfString
     * @return
     */
    public static String unicodeToChina(String utfString) {
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;

        while ((i = utfString.indexOf("\\u", pos)) != -1) {
            sb.append(utfString.substring(pos, i));
            if (i + 5 < utfString.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(utfString.substring(i + 2, i + 6), 16));
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        String line = "张三李四王五赵六 ABCabc !@#$%^&*()_+{}:<>?,.;'][-=  1234567890 䬅 \\u5f20";

        //转义所有字符串
//        String str = StringToUnicode(line);
//        System.out.println(str);
//        System.out.println(UnicodeToString(str));

        //只转义中文
        String a = chinaToUnicode(line);
        System.out.println(a);
        System.out.println(unicodeToChina(a));


        //判断是否生僻字
        System.out.println(isShengpizi("䬅"));
        System.out.println(isShengpizi("啊啊"));
    }
}
